<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    // 如果让promise发挥作用，是需要封装函数的
    // const promise = new Promise((resolve, reject) => {
    //   setTimeout(() => {
    //     console.log("2000");
    //     resolve();
    //   }, 2000)
    // });

    // promise.then(() => {
    //   console.log("promise已经成功了");
    // });




    // 封装函数
    const fn = (time) => {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          console.log(time);
          resolve();
        }, time)
      });
    }
    fn(3000)
    .then(() => {
      console.log("promise已经成功了");
      fn(2000)
      .then(() => {
        console.log("promise已经又成功了");
      })
    })




    // 数据请求
    const getData = (url) => {
      return new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest();
        xhr.open('GET', url);
        xhr.send();
        xhr.onload = function() {
          if (xhr.status === 200) {
            // console.log(JSON.parse(xhr.responseText));
            resolve(JSON.parse(xhr.responseText))
          } else {
            console.error('请求失败', xhr.status);
          }
        };
      })
    }
    getData("https://cnodejs.org/api/v1/topics")
    .then((res) => {
      console.log(res);
    })
    .then(() => {
      getData('https://cnodejs.org/api/v1/topic/61f69e4aa08b39f75309c2a8')
      .then((res) => {
        console.log(res);
      })
    })

    



    
    
    
  </script>
</body>
</html>